//
//  FeedbackController2.swift
//  YiYaoHui
//
//  Created by HM on 16/1/4.
//  Copyright © 2016年 TMPercy. All rights reserved.
//

import UIKit

class FeedbackController2: UIViewController {

    @IBOutlet weak var feedbackTableView: UITableView!
    
    var feedbackCell: FeedbackCell!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardShow:", name: UIKeyboardDidShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardHide:", name: UIKeyboardDidHideNotification, object: nil)
        feedbackTableView.scrollEnabled = false
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
        tabBarController?.tabBar.hidden = false
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    func keyboardShow(notification: NSNotification) {
        feedbackTableView.scrollEnabled = true
    }
    
    func keyboardHide(notification: NSNotification) {
        feedbackTableView.scrollEnabled = false
    }
}

extension FeedbackController2: UITableViewDataSource {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("feedbackCell", forIndexPath: indexPath) as! FeedbackCell
        cell.selectionStyle = .None
        feedbackCell = cell
        return cell
    }
}

extension FeedbackController2: UITableViewDelegate {
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return self.view.frame.height + 100.0
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        dismissKeyboard()
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    func dismissKeyboard() {
        feedbackTableView.setContentOffset(CGPoint(x: 0, y: -64), animated: true)
        feedbackCell.adviceTextView.resignFirstResponder()
        feedbackCell.phoneTextField.resignFirstResponder()
        feedbackCell.QQTextField.resignFirstResponder()
    }
}
